package com.rover022.flixelgame.manager
{
	import com.rover022.flixelgame.ui.BaseUI;
	import com.rover022.flixelgame.ui.battle.ChoiceBattlePop;
	import com.rover022.flixelgame.ui.equippackage.EquipPackage;
	import com.rover022.flixelgame.ui.equipupgrade.EquipUpGrade;
	import com.rover022.flixelgame.ui.heroresolve.HerosPackage;
	import com.rover022.flixelgame.ui.heroresolve.PlayInfoAndMenuUI;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import ghostcat.manager.RootManager;
	import ghostcat.ui.PopupManager;
	
	import org.flixel.FlxG;
	import org.flixel.FlxState;

	/**
	 * 场景切换器
	 * 主要控制在不同的场景的时候控制主界面UI的布局和显示..
	 * 兼职UI管理器
	 * 兼职图层管理器
	 * @author flashpig
	 */
	public class WorldManager
	{
		public static var CITY:String="city";
		public static var BATTLE:String="battle";
		public var playerInfoUI:PlayInfoAndMenuUI;
		//public var battleInfoUI:BattleInfoUI;
		/**
		 * 当前打开的UI层;
		 */
		public var currentUIArray:Vector.<Sprite>=new Vector.<Sprite>;
		private static var _instance:WorldManager;

		public static function getInstance():WorldManager
		{
			if (null == _instance)
			{
				_instance=new WorldManager();
			}
			return _instance;
		}

		/**
		 * 进入何种场景  切换相关的UIlay;
		 * 在城镇应该出现的是玩家的 playerInfoUI
		 * 在战场应该出现的是战场的UI battleInfoUI
		 * @param mode
		 * @param _state
		 */
		public function gotoWorld(mode:String, _state:FlxState):void
		{
			switch (mode)
			{
				case CITY:
				{
					holdAllUI();
					if (null == playerInfoUI)
					{
						playerInfoUI=new PlayInfoAndMenuUI();
					}
					showUI(playerInfoUI);
					break;
				}
				case BATTLE:
				{
					holdAllUI();
					break;
				}
				default:
				{
					break;
				}
			}
			FlxG.switchState(_state);
		}

		/**
		 *  移除所有在所有在uiLay显示的UI界面
		 */
		public function holdAllUI():void
		{
			var contain:Sprite=RootManager.getValue("uiLay");
			contain.removeChildren();
		}

		/**
		 * 让场景相关的UI出现
		 * @param playerInfoUI
		 *
		 */
		public function showUI(_sprite:Sprite):void
		{
			if (null == _sprite.parent)
			{
				var contain:Sprite=RootManager.getValue("uiLay");
				contain.addChild(_sprite);
			}
		}

		/**
		 * 重要显示什么类型的UI
		 * 根据name 显示不同 的显示面板元件..
		 * @param uiname
		 *
		 */
		public function showPop(uiname:String):void
		{
			var panel:BaseUI;
			trace(uiname);
			switch (uiname)
			{
				case "heroreSolve": //所有英雄UI
				{

					panel=new HerosPackage();
					PopupManager.instance.showPopup(panel, null);
					break;
				}
				case "itemButton": //所有道具UI
				{

					panel=new EquipPackage();
					PopupManager.instance.showPopup(panel, null);
					break;
				}
				case "equipupButton": //装备升级UI
				{

					panel=new EquipUpGrade();
					PopupManager.instance.showPopup(panel, null);
					break;
				}
				case "battleButton":
				{
					panel=new ChoiceBattlePop();
					PopupManager.instance.showPopup(panel, null);
					break;
				}
				default:
				{
					break;
				}
			}
		}

		/**
		 * 根据弹出窗口的名字来寻找弹出窗口;
		 * @param _name
		 *
		 */
		public function findPopByName(_name:String):DisplayObject
		{
			var array:Array=PopupManager.instance.popups;
			for (var i:int=0; i < array.length; i++)
			{
				var obj:DisplayObject=array[i];
				if (obj.name == _name)
				{
					return obj;
				}
			}
			return null;
		}
	}
}
